from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QRegExpValidator


global __rag


def QLinedit_only_num() -> QRegExpValidator:
    """
    QLinedit只允许输入数字
    @rtype: object
    """
    reg = QRegExp("[0-9]+$")
    validator = QRegExpValidator()
    validator.setRegExp(reg)
    return validator


def QLinedit_sum_letter() -> QRegExpValidator:
    """
    设置文本仅可输入数字和字母
    @return:
    """
    reg = QRegExp("[a-zA-Z0-9]+$")
    validator = QRegExpValidator()
    validator.setRegExp(reg)
    return validator


def QLinedit_format(is_num=True, is_letter=True) -> QRegExpValidator or None:
    """
    设置格式
    @param is_num: 允许数字
    @param is_letter: 允许字母
    @return:
    """
    global __rag
    __rag = None
    if not is_num and not is_letter:
        pass
    if is_num and is_letter:
        __rag = "[a-zA-Z0-9]+$"
    elif is_num and not is_letter:
        __rag = "[0-9]+$"
    elif not is_num and is_letter:
        __rag = "[a-zA-Z]+$"
    reg = QRegExp(__rag)
    validator = QRegExpValidator()
    validator.setRegExp(reg)
    return validator
